//
//  NSDictionary+FLRequest.m
//  Cat
//
//  Created by lushitong on 2018/12/1.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "NSDictionary+FLRequest.h"

@implementation NSDictionary (FLRequest)

/// 请求加密
+ (NSDictionary *)encodeRequestPararms:(NSDictionary *)params {
	NSMutableDictionary *dict = params ? [NSMutableDictionary dictionaryWithDictionary:params] : [NSMutableDictionary dictionary];
	return dict;
}

/// 返回结果解密
+ (NSDictionary *)decodeRequestResult:(NSDictionary *)resultDict{
	
	if(!resultDict[@"data"]) {return resultDict;}
	
	NSString *beforeBase64MD5 = resultDict[@"data"];
	
	/// 非加密类型，直接返回data
	if(![beforeBase64MD5 isKindOfClass:[NSString class]]) {
		return resultDict;
	}
	
	NSString *afterDeMd5 = [NSString decryptDES:beforeBase64MD5];
	
	NSData *jsonData = [afterDeMd5 dataUsingEncoding:NSUTF8StringEncoding];
	NSError *err = nil;
	NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
	
	if(err) {return resultDict; }
	
	return [self composeParseDict:dict withResultDict:resultDict];
	
}

+ (NSDictionary *)composeParseDict:(NSDictionary *)parseDic withResultDict:(NSDictionary *)resultDict {
	NSMutableDictionary *dict = [NSMutableDictionary dictionary];
	dict[@"data"] = parseDic;
	dict[@"msg"] = resultDict[@"msg"];
	dict[@"code"] = resultDict[@"code"];
	
	return dict;
}

+ (BOOL)isNullOrNilWithObject:(id)object;
{
	if (object == nil || [object isEqual:[NSNull null]]) {
		return YES;
	}
	return NO;
}


@end
